﻿//jqwidgets交互
$(function () {
	$('#jqxTabs').jqxTabs({
		width: '100%',
		position: 'top',
		animationType: 'fade',
		selectionTracker: 'checked',
		showCloseButtons: true,
		reorder: true,
		theme: 'arctic',
		scrollPosition: 'both'
	});
    //自适应
	$('.iframe').height($(window).height() - 180);

    //注入beforeRemove
	var f = $.jqx._jqxTabs.prototype.removeAt;
	$.jqx._jqxTabs.prototype.removeAt = function () {
	    if (this.host.triggerHandler("beforeRemove", self) !== false)
	        f.apply(this, arguments);
	};
});

//自适应
$(window).resize(function () {
	$('.iframe').height($(window).height() - 180);
});

//动态追加Tabs
function addTab(icon, title, url) {
	//去重
	var index = getIndex(title)
	if (index != undefined) {
		$('#jqxTabs').jqxTabs('select', index);
	}
	else {
		icon = '<i class="' + icon + ' icon"></i>';
		var _h = $(window).height() - 180;
		var frm = '<iframe class="iframe" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:' + _h + 'px"></iframe>'
		$('#jqxTabs').jqxTabs('addLast', icon + title, frm);
	}
}

//根据Title获取Index
function getIndex(title) {
	var num = $('#jqxTabs').jqxTabs('length');
	var index;
	for (var i = 0; i < num; i++) {
		var exist = $('#jqxTabs').jqxTabs('getTitleAt', i);
		if (exist == title) {
			index = i;
		}
	}
	return index;
}

//关闭之前
$("#jqxTabs").on("beforeRemove", function (event) {
    var disabledItems = $('#jqxTabs').jqxTabs('getDisabledTabsCount');
    var items = $('#jqxTabs').jqxTabs('length');
    if (items > disabledItems + 1) {
        return true;
    }
    else {
        return false;
    }
})